package com.xja.seckill.order.service.impl;

import com.xja.seckill.order.commons.JsonBean;
import com.xja.seckill.order.ProductCilentApi;
import com.xja.seckill.order.domain.mapper.OrderDetailMapper;
import com.xja.seckill.order.domain.mapper.OrderMapper;
import com.xja.seckill.order.domain.model.Order;
import com.xja.seckill.order.domain.model.OrderDetail;
import com.xja.seckill.order.rabbitmq.RabbitMQOrderSend;
import com.xja.seckill.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitMQOrderSend rabbitMQOrderSend;

    @Autowired
    private ProductCilentApi productCilentApi;


    @Override
    public JsonBean createOrder(Integer userId, Integer skuId) {
//        Integer count = (Integer) redisTemplate.opsForValue ().get ("productCount-" + skuId);
        Long count = redisTemplate.opsForValue ().increment ("productCount-" + skuId,-1);
        System.out.println (count);
        if (count==null || count< 0) {
            Long increment = redisTemplate.opsForValue ().increment ("productCount-" + skuId, 1);
            return new JsonBean(-1,"商品库存不足",null);
        } else {
            productCilentApi.reducerStock (skuId);
        }
//      生成订单并入库
        Order order = new Order ();
        order.setCode (UUID.randomUUID().toString().replaceAll("-","").substring (0,9));
        order.setType ((byte) 1);
        order.setPaymentType ((byte) 1);
        order.setStatus ((byte) 1);
        order.setCreateTime (new Date ());
        order.setIsDeleted (false);
        order.setAmount (new BigDecimal (555));
        int insert = orderMapper.insert(order);
//      生成订单详情并入库
        OrderDetail orderDetail = new OrderDetail ();
        orderDetail.setOrderId (order.getId ());
        orderDetail.setSkuId (skuId);
        orderDetail.setPrice (new BigDecimal (666));
        orderDetail.setActualPrice (new BigDecimal (555));
        orderDetail.setNum (6);
        orderDetail.setIsDeleted (false);
        int insert1 = orderDetailMapper.insert(orderDetail);
//      订单写入Redis
        if (insert1>0 && insert>0){
            redisTemplate.opsForValue ().set ("orderId"+order.getId (),order);
        }else {
            return new JsonBean(-2,"订单生成失败",null);
        }
//        订单写入rabbitmq
        rabbitMQOrderSend.sendOrder (order);
        return new JsonBean(0,"生成订单成功",null);
    }

    @Override
    public void    payOrder(String code) {
        Order order = orderMapper.selectByCodes (code);
        order.setStatus ((byte) 2);
        orderMapper.updateByPrimaryKeySelective (order);
    }


//    private int createOrderId(Integer userId){
//        return  Integer.parseInt (userId+System.currentTimeMillis()+UUID.randomUUID ().toString ().replace ("-", "").substring (0, 6));
//    }

}
